home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 421_01 / zoom.cpp < prev    next >
C/C++ Source or Header  |  1994-01-16  |  11KB  |  595 lines

  1. // ZOOM.CPP
  2.  
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include <dos.h>
  6. #include "stdio.h"
  7. #include "timer.h"
  8. #include "vx.h"
  9.  
  10. #define SLOW        0
  11. #define MSEKDELAY 50
  12.  
  13. //#define NOTUNE
  14. //#define NODRAW
  15.  
  16. #define FRAMES    44    // Number of times the resolution is doubled
  17. #define EXT     192    // Size of window, in pixels
  18. #define ROUGHSPLITS 12
  19. #define FRACX   0
  20. #define FRACY   0
  21.  
  22. #if EXT>96
  23.     #define SLOW 0
  24. #endif
  25.  
  26. extern timer_C timer;
  27. extern vx_C vx;
  28. extern byte *keypressed;
  29.  
  30. // GLOBALS
  31.  
  32. word zoomvramptr=0xa000;
  33. static byte *bitdata[FRAMES-1],*frac1b,*frac2b,*frac3b,*frac1,*frac2,*frac3;
  34. word datalengths[FRAMES-1];
  35. word innerlen,innerleft,innertop,innerfac,inneradr,farfac;
  36. byte startx,starty,nearstartx,nearstarty;
  37. byte *globalbmp;
  38.  
  39. // SETZCOLORS
  40.  
  41. void setzcolors(void)
  42. {
  43.     for (int teller=1; teller<256; teller++)
  44.     {
  45.         float r=sin(teller*1.2*M_PI/255);
  46.         float g=sin(teller*1.8*M_PI/255);
  47.         float b=sin(teller*2.4*M_PI/255);
  48.  
  49.         r=fabs(r)*63;
  50.         g=fabs(g)*63;
  51.         b=fabs(b)*63;
  52.  
  53.         vx.setrgb(teller,r,g,b);
  54.     }
  55. }
  56.  
  57. // ROUGHSPLIT
  58.  
  59. void roughsplit(byte *from, byte *to,word stripno)
  60. {
  61.     long from_ptr=(long)from;
  62.     long to_ptr=(long)to;
  63.     to_ptr+=EXT*stripno*(EXT/ROUGHSPLITS);
  64.     from_ptr+=(EXT/4)+(EXT/4)*EXT+(EXT*stripno*(EXT/ROUGHSPLITS)/2);
  65.  
  66.     asm pusha
  67.     asm push ds
  68.  
  69.     asm cld
  70.     asm les di,to_ptr
  71.     asm lds si,from_ptr
  72.  
  73.     asm mov bx,es
  74.     asm mov ch,(EXT/ROUGHSPLITS)/2
  75. nextline:
  76.     asm mov cl,EXT/2
  77. nextpixel: // unroll!
  78.     asm lodsb
  79.     asm stosb
  80.     asm stosb
  81.     asm dec cl
  82.     asm jnz nextpixel
  83.     asm add di,EXT
  84.     asm add si,EXT/2
  85.     asm dec ch
  86.     asm jnz nextline
  87.     asm lds si,to_ptr
  88.     asm mov di,si
  89.     asm mov ax,EXT
  90.     asm add di,ax
  91.     asm mov dl,(EXT/ROUGHSPLITS)/2
  92. twinline:
  93. #if USE32BIT
  94.     asm mov cx,EXT/4
  95.     asm rep movsd
  96. #else
  97.     asm mov cx,EXT/2
  98.     asm rep movsw
  99.     asm add si,ax
  100.     asm add di,ax
  101. #endif
  102.     asm dec dl
  103.     asm jnz twinline
  104.     asm pop ds
  105.     asm popa
  106.  
  107. }
  108.  
  109. byte *datas,datamask,lastpopped;
  110.  
  111. // POPBIT
  112.  
  113. byte _fastcall popbit(void)
  114. {
  115.     asm les bx,dword ptr datas
  116.     asm mov al,[es:bx]
  117.     asm and al,datamask
  118.     asm shr datamask,1
  119.     asm jnc ok
  120.     asm mov datamask,128
  121.     asm inc word ptr datas
  122. ok:
  123. }
  124.  
  125. // CTEST
  126.  
  127. void near _pascal ctest(int x,int y,byte len)
  128. {
  129. #ifdef NOTUNE
  130.     return;
  131. #endif
  132.     static byte p,fac;
  133.     int yy,xx;
  134.     asm les bx,dword ptr datas    // es = kontant ptr->datas
  135.     asm mov yy,2
  136. newx:
  137.     asm mov xx,2
  138. nextx:
  139.     asm les bx,dword ptr datas    // es = kontant ptr->datas
  140.     asm mov al,[es:bx]
  141.     asm and al,datamask
  142.     asm shr datamask,1
  143.     asm jnc ok
  144.     asm mov datamask,128
  145.     asm inc word ptr datas
  146. ok:
  147.     asm and al,al
  148.     asm jnz doit
  149.     goto staylow;
  150. doit:
  151.     if (len>2)
  152.     {
  153.         static word nx,ny;
  154.         word len2=len;
  155.         len2>>=1;
  156.         nx=0;
  157.         ny=0;
  158.         if (xx==1)
  159.             nx=len2;
  160.         if (yy==1)
  161.             ny=len2;
  162.         ctest(x+nx,y+ny,len2);
  163.     }
  164.     else
  165.     {
  166.         byte color=0;
  167.         asm les bx,dword ptr datas    // es = kontant ptr->datas
  168.         asm mov al,[es:bx]
  169.         asm and al,datamask
  170.         asm shr datamask,1
  171.         asm jnc againok
  172.         asm mov datamask,128
  173.         asm inc word ptr datas
  174. againok:
  175.         asm and al,al
  176.         asm jz notvip
  177.  
  178.             if (popbit())
  179.                 color=1;
  180.             else
  181.                 color=-1;
  182.             goto vipdone;
  183. notvip:
  184.             color=0;
  185.             p=popbit();
  186.             fac=1;
  187.             if (popbit())    color+=fac;    fac<<=1;
  188.             if (popbit())    color+=fac;    fac<<=1;
  189.             if (popbit())    color+=fac;    fac<<=1;
  190.             if (p)
  191.                 color+=2;
  192.             else
  193.             {
  194.                 color+=247;
  195.                 if (color==247)
  196.                 {
  197.                     fac=1;
  198.                     color=0;
  199.                     if (popbit())    color+=fac;    fac<<=1;
  200.                     if (popbit())    color+=fac;    fac<<=1;
  201.                     if (popbit())    color+=fac;    fac<<=1;
  202.                     if (popbit())    color+=fac;    fac<<=1;
  203.                     if (popbit())    color+=fac;    fac<<=1;
  204.                     if (popbit())    color+=fac;    fac<<=1;
  205.                     if (popbit())    color+=fac;    fac<<=1;
  206.                     if (popbit())    color+=fac;    fac<<=1;
  207.                 }
  208.             }
  209. vipdone:
  210.         globalbmp[(x+2-xx)*EXT+y+2-yy]+=color;
  211.     }
  212. staylow:
  213.     asm dec xx
  214.     asm jz xdone
  215.     asm jmp nextx
  216. xdone:
  217.     asm dec yy
  218.     asm jz ydone
  219.     asm jmp newx
  220. ydone:
  221. }
  222.  
  223. // FINEMASK
  224.  
  225. void finemask(byte *bits,byte *bmp,word no)
  226. {
  227.     static long bit_ptr;
  228.     bit_ptr=(long)bits;
  229.     if (!no)
  230.         datas=(byte *)bit_ptr;
  231.     globalbmp=bmp;
  232.     datamask=128;
  233.         ctest((no%6)*(EXT/6),(no/6)*(EXT/6),(EXT/6));
  234.     while (datamask<0x80)
  235.     popbit();
  236. }
  237.  
  238.  
  239.  
  240. // DRAWNEAR
  241.  
  242. void drawnear(word sx,word sy,word height, word width,long bmp_ptr)
  243. {
  244. #ifdef NODRAW
  245.     return;
  246. #endif
  247.     if (!width || !height)
  248.         return;
  249.  
  250.     byte nearstarty_ss=nearstarty;
  251.     word start,startdi;
  252.     byte br0kx=nearstartx;
  253.  
  254.     asm pusha
  255.     asm push ds
  256.  
  257.     asm mov dx,zoomvramptr
  258.     asm mov es,dx
  259.     asm mov dx,sx
  260.     asm mov cx,dx
  261.     asm shr dx,2
  262.     asm mov di,dx
  263.     asm mov dx,sy
  264.     asm shl dx,4
  265.     asm add di,dx
  266.     asm add dx,dx
  267.     asm add dx,dx
  268.     asm add di,dx // di = screenpos
  269.  
  270.     asm mov bh,byte ptr innerfac // bx = innerfac
  271.     asm lds si,bmp_ptr
  272.     asm mov ch,byte ptr height // ch = y-teller
  273.     asm and cl,3
  274. next_strip:
  275.     asm mov start,si
  276.     asm mov ax,0x0100+MAP_MASK
  277.     asm shl ah,cl
  278.     asm mov dx,SC_INDEX
  279.     asm out dx,ax
  280.  
  281.     asm mov startdi,di
  282.     asm mov dx,79
  283.     asm mov bl,byte ptr width // bl = x-teller, ah = br0k-y
  284.     asm mov al,nearstarty_ss
  285. next_down: // unroll!
  286.     asm movsb
  287.     asm add di,dx
  288.     asm add ah,bh
  289.     asm adc si,0
  290.     asm dec bl
  291.     asm jnz next_down
  292.     asm mov si,start
  293.     asm add si,EXT
  294.     asm add br0kx,bh
  295.     asm jnc nomoreright
  296.     asm add si,EXT
  297. nomoreright:
  298.     asm mov di,startdi
  299.     asm inc cl
  300.     asm test cl,4
  301.     asm jz dontadvanceright
  302.     asm inc di
  303.     asm xor cl,cl
  304. dontadvanceright:
  305.     asm dec ch
  306.     asm jnz next_strip
  307.  
  308.     asm pop ds
  309.     asm popa
  310. }
  311.  
  312. // DRAWFAR
  313.  
  314. void drawfar(word sx,word sy,word height, word width,long bmp_ptr)
  315. {
  316. #ifdef NODRAW
  317.     return;
  318. #endif
  319.     if (!width || !height)
  320.         return;
  321.  
  322.     word start,startdi;
  323.     byte br0kx=startx;
  324.     byte starty_ss=starty;
  325.  
  326.     asm pusha
  327.     asm push ds
  328.  
  329.     asm mov dx,zoomvramptr
  330.     asm mov es,dx
  331.     asm mov dx,sx
  332.     asm mov cx,dx
  333.     asm shr dx,2
  334.     asm mov di,dx
  335.     asm mov dx,sy
  336.     asm shl dx,4
  337.     asm add di,dx
  338.     asm add dx,dx
  339.     asm add dx,dx
  340.     asm add di,dx // di = screenpos
  341.  
  342.     asm mov bh,byte ptr farfac // bx = innerfac
  343.     asm lds si,bmp_ptr
  344.     asm mov ch,byte ptr height // ch = y-teller
  345.     asm and cl,3
  346. next_strip:
  347.     asm mov start,si
  348.     asm mov ax,0x0100+MAP_MASK
  349.     asm shl ah,cl
  350.     asm mov dx,SC_INDEX
  351.     asm out dx,ax
  352.  
  353.     asm mov startdi,di
  354.     asm mov dx,80
  355.     asm mov al,byte ptr width // al = x-teller, ah = br0k-y
  356.     asm mov ah,starty_ss
  357. next_down: // unroll!
  358.     asm mov bl,[si]
  359.     asm mov [es:di],bl
  360.     asm add di,dx
  361.     asm add ah,bh
  362.     asm adc si,0
  363.     asm dec al
  364.     asm jnz next_down
  365.     asm mov si,start
  366.     asm add br0kx,bh
  367.     asm jnc nomoreright
  368.     asm add si,EXT
  369. nomoreright:
  370.     asm mov di,startdi
  371.     asm inc cl
  372.     asm test cl,4
  373.     asm jz dontadvanceright
  374.     asm inc di
  375.     asm xor cl,cl
  376. dontadvanceright:
  377.     asm dec ch
  378.     asm jnz next_strip
  379.  
  380.     asm pop ds
  381.     asm popa
  382.     return;
  383. singleline:
  384. }
  385.  
  386. // CONV
  387.  
  388. word conv(int x,int y,int tune)
  389. {
  390.     float fx,fy;
  391.  
  392.     float nearmov=tune/48.0;
  393.     float mov=nearmov/2;
  394.  
  395.     fx=x-FRACX-EXT/2;
  396.     fy=y-FRACY-EXT/2;
  397.     fx/=EXT/2.0;
  398.     fy/=EXT/2.0;
  399.     float fxx=fx*(EXT/2)*(1-mov)+EXT/2;
  400.     float fyy=fy*(EXT/2)*(1-mov)+EXT/2;
  401.  
  402.     int xxx=fxx;
  403.     int yyy=fyy;
  404.     fxx-=xxx;
  405.     fyy-=yyy;
  406.     startx=255.99*fxx;
  407.     starty=255.99*fyy;
  408.     farfac=255.99*(1-mov);
  409.  
  410.     return yyy+EXT*xxx;
  411. }
  412.  
  413. // SETZOOM
  414.  
  415. void setzoom(int zfac)
  416. {
  417.     if (!zfac)
  418.     {
  419.         innerleft=FRACX+EXT/4;
  420.         innertop=FRACY+EXT/4;
  421.         innerlen=EXT/2+1;
  422.         innerfac=255;
  423.         return;
  424.     }
  425.     float mov=zfac/48.0;
  426.     float innerlenf=EXT/(2.0-mov);
  427.     innerlen=innerlenf;
  428.     float realinnerleft=FRACX+EXT/2-innerlen/2;
  429.     float realinnertop=FRACY+EXT/2-innerlen/2;
  430.     innertop=realinnertop;
  431.     innerleft=realinnerleft;
  432.     realinnerleft-=innerleft;
  433.     realinnertop-=innertop;
  434.     nearstartx=0;
  435.     nearstarty=0;
  436.     inneradr=innerleft+80*innertop;
  437.     innerfac=255*(2.0-mov);
  438.     innerfac-=256;
  439. }
  440.  
  441. // DRAWZOOM
  442.  
  443. void drawzoom(long frac1,long frac2,int tune1)
  444. {
  445.     float ftune=(tune1/48.0);
  446.     drawnear(innerleft,innertop,innerlen,innerlen,frac2);
  447.     int tlpluss,trpluss,cupluss,cdpluss;
  448.     tlpluss=conv(0,0,tune1);
  449.     if (tune1>0)
  450.     {
  451.         drawfar(FRACX,FRACY,EXT/2-innerlen/2,EXT,frac1+tlpluss);
  452.         cupluss=conv(innerleft,FRACY,tune1);
  453.         drawfar(innerleft,FRACY,innerlen,EXT/2-innerlen/2,frac1+cupluss);
  454.         trpluss=conv(FRACX+EXT/2+innerlen/2,FRACY,tune1);
  455.